<template>
  <el-dialog
    :visible.sync="visiable"
    title="新增留言"
    width="606px"
    custom-class="leave-msg-body"
    append-to-body
  >
    <div class="leave-msg-pop">
      <span class="title">平台留言</span>
      <el-form
        :model="ruleForm"
        label-position="left"
        :rules="rules"
        ref="ruleForm"
        label-width="105px"
        class="demo-ruleForm"
      >
        <el-form-item label="标题" prop="title">
          <el-input v-model="ruleForm.title"></el-input>
        </el-form-item>
        <el-form-item label="留言类型" prop="type">
          <el-select
            v-model="ruleForm.type"
            placeholder="请选择留言类型"
            style="width: 100%"
          >
            <el-option
              :label="item.name"
              :value="item.id"
              v-for="(item, i) in typeList"
              :key="i"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="联系人名称" prop="studentName">
          <el-input v-model="ruleForm.studentName"></el-input>
        </el-form-item>
        <el-form-item label="联系人电话" prop="phone">
          <el-input v-model="ruleForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="remark">
          <el-input
            type="textarea"
            :row="5"
            v-model="ruleForm.remark"
          ></el-input>
        </el-form-item>
        <el-form-item label="上传文件" prop="fileUrl">
          <PdfUpload
            v-model="ruleForm.fileUrl"
            :is-show-tip="true"
            :fileType="['zip']"
            :limit="1"
            :file-size="500"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            class="btn cancel"
            @click="resetForm('ruleForm'), (visiable = false)"
            >取消</el-button
          >
          <el-button
            class="btn confirm"
            type="primary"
            @click="submitForm('ruleForm')"
            :disabled="btnDisabled"
            >提交</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </el-dialog>
</template>
<script>
import PdfUpload from "./components/PdfUpload";
export default {
  name: "AddMsgPop",
  props: ["typeList"],
  components: { PdfUpload },
  data() {
    return {
      visiable: false,
      name: "",
      region: "",
      date1: "",
      date2: "",
      delivery: false,
      type: [],
      resource: "",
      desc: "",
      ruleForm: {},
      btnDisabled: true,
      rules: {
        title: [
          { required: true, message: "请输入标题", trigger: "blur" },
          {
            min: 1,
            max: 20,
            message: "长度要求 1 到 20 个字符",
            trigger: "blur",
          },
        ],
        studentName: [
          { required: true, message: "请输入联系人名称", trigger: "blur" },
          {
            min: 1,
            max: 15,
            message: "长度要求 1 到 15 个字符",
            trigger: "blur",
          },
        ],
        phone: [
          { required: true, message: "请输入联系人电话", trigger: "blur" },
          {
            min: 1,
            max: 20,
            message: "长度要求 1 到 20 个字符",
            trigger: "blur",
          },
        ],
        type: [
          {
            required: true,
            message: "请至少选择一种留言类型",
            trigger: "change",
          },
        ],
        remark: [
          { required: true, message: "请输入描述内容", trigger: "blur" },
          {
            min: 2,
            max: 500,
            message: "长度要求 2 到 500 个字符",
            trigger: "blur",
          },
        ],
      },
      fileUrl: "",
    };
  },
  watch: {
    ruleForm: {
      handler(val) {
        if (
          val.title &&
          val.type &&
          val.studentName &&
          val.phone &&
          val.remark
        ) {
          this.btnDisabled = false;
        } else {
          this.btnDisabled = true;
        }
      },
      deep: true,
    },
  },
  methods: {
    open() {
      this.visiable = true;
      this.resetForm("ruleForm");
    },
    submitForm(formName) {
      console.log(this.ruleForm, "----this.ruleForm0000");
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$emit("handleSubmit", {
            params: {
              ...this.ruleForm,
              leaveType: 1,
            },
            done: () => {
              this.visiable = false;
            },
          });
        } else {
          this.btnDisabled = true;
        }
      });
    },
    close() {
      this.visiable = false;
    },
    resetForm(formName) {
      this.$refs[formName] && this.$refs[formName].resetFields();
    },
  },
};
</script>

<style lang="less">
.leave-msg-body {
  .el-dialog__header {
    padding: 20px 37px 20px 15px;
    font-size: 18px;
    font-family: PingFangSC-Semibold, PingFang SC;
    font-weight: 600;
    color: #37383d;
    line-height: 25px;
  }
  .el-dialog__body {
    padding: 0 37px 0 15px;
  }
  .leave-msg-pop {
    border-top: 1px solid #d4d6da;
    padding-top: 16px;
    padding-bottom: 20px;
    .title {
      font-size: 16px;
      font-family: PingFangSC-Regular, PingFang SC;
      font-weight: 400;
      color: #37383d;
    }
    .el-form {
      margin-top: 15px;

      .el-form-item__label {
        font-size: 16px;
        font-family: PingFangSC-Regular, PingFang SC;
        font-weight: 400;
        color: #37383d;
      }

      .btn {
        width: 168px;
        height: 45px;
        text-align: center;
        border-radius: 3px;
        font-size: 16px;
      }

      .cancel {
        margin-right: 28px;

        font-family: PingFangSC-Semibold, PingFang SC;
        font-weight: 600;
        color: #4677b9;
        border: 1px solid #4677b9;
      }

      .el-button--primary.is-disabled {
        background: #cecece;
        font-family: PingFangSC-Semibold, PingFang SC;
        font-weight: 600;
        color: #ffffff;
        border: 1px solid #cecece;
      }
      .el-button--primary {
        background: #3d6cb0;
        border-color: #3d6cb0;
      }
    }
  }
}
</style>
